import requests
import aiohttp
import asyncio
from datetime import datetime

# todo aiohttp 和 requests
# requests 版爬虫
# def fetch(url):
#     r = requests.get(url)
#     print(r.text)
#
# start = datetime.now()
#
# for i in range(30):
#     fetch('http://httpbin.org/get')
#
# end = datetime.now()
#
# print("requests版爬虫花费时间为：")
# print(end - start)

# async def fetch(client):
#     async with client.get('http://httpbin.org/get') as resp:
#         assert resp.status == 200
#         return await resp.text()
#
#
# async def main():
#     async with aiohttp.ClientSession() as client:
#         html = await fetch(client)
#         print(html)
#
# loop = asyncio.get_event_loop()
#
# tasks = []
# for i in range(30):
#     task = loop.create_task(main())
#     tasks.append(task)
#
# start = datetime.now()
#
# loop.run_until_complete(main())
#
# end = datetime.now()
#
# print("aiohttp版爬虫花费时间为：")
# print(end - start)

# todo 同一个 session
'''
aiohttp.ClientSession() 中封装了一个 session 的连接池，并且在默认情况下支持 keepalives，
官方建议在程序中使用单个 ClientSession 对象，而不是像上面示例中的那样每次连接都创建一个 ClientSession 对象，除非在程序中遇到大量的不同的服务
'''
# async def fetch(client):
#     print("打印 ClientSession 对象")
#     print(client)
#     async with client.get('http://httpbin.org/get') as resp:
#         assert resp.status == 200
#         return await resp.text()
#
#
# async def main():
#     async with aiohttp.ClientSession() as client:
#        tasks = []
#        for i in range(30):
#            tasks.append(asyncio.create_task(fetch(client)))
#        await asyncio.wait(tasks)
#
# loop = asyncio.get_event_loop()
#
# start = datetime.now()
#
# loop.run_until_complete(main())
#
# end = datetime.now()
# print("aiohttp版爬虫花费时间为：")
# print(end - start)

# todo 返回值
# json串
# async def fetch(client):
#     async with client.get('http://httpbin.org/get') as resp:
#         return await resp.json()

# 字节流
# async def fetch(client):
#     async with client.get('http://httpbin.org/image/png') as resp:
#         return await resp.read() # 可以传递数字参数用于读取多少个字节
#
#
# async def main():
#     async with aiohttp.ClientSession() as client:
#         image = await fetch(client)
#         with open("image.png", 'wb') as f:
#             f.write(image)

# todo 参数 种方式在 URL 地址中传递参数
# 1 列表
# async def fetch(client):
#     params = [('a', 1), ('b', 2)]
#     async with client.get('http://httpbin.org/get',params=params) as resp:
#         return await resp.text()

# 2 字典
# async def fetch(client):
#     params = {"a": 1,"b": 2}
#     async with client.get('http://httpbin.org/get',params=params) as resp:
#         return await resp.text()

# 3 直接拼接
# async def fetch(client):
#     async with client.get('http://httpbin.org/get',params='q=aiohttp+python&a=1') as resp:
#         return await resp.text()

# todo 请求头
# headers参数
# async def fetch(client):
#     headers = {'content-type': 'application/json', 'User-Agent': 'Python/3.7 aiohttp/3.7.2'}
#     async with client.get('http://httpbin.org/get',headers=headers) as resp:
#         return await resp.text()

# COOKIES cookies 是整个会话共用的，所以应该在初始化 ClientSession 对象时传递
# async def fetch(client):
#     async with client.get('http://httpbin.org/get') as resp:
#         return await resp.text()
#
#
# async def main():
#     cookies = {'cookies': 'this is cookies'}
#     async with aiohttp.ClientSession(cookies=cookies) as client:
#         html = await fetch(client)
#         print(html)

# todo POST 方式
async def fetch(client):
    data = {'a': '1', 'b': '2'}
    async with client.post('http://httpbin.org/post', data = data) as resp:
        return await resp.text()

# todo 超时
async def fetch(client):
    async with client.get('http://httpbin.org/get', timeout=60) as resp:
        return await resp.text()